<template>
  <div class="modal-overlay" @click="close">
    <div class="modal-content select-index-modal" @click.stop>
      <div class="modal-header">
        <h2>选择要编辑的索引</h2>
        <button @click="close" class="close-btn">&times;</button>
      </div>
      
      <div class="modal-body">
        <div v-if="indices.length === 0" class="no-indices">
          没有可用的索引
        </div>
        
        <div v-else class="indices-list">
          <div 
            v-for="index in indices" 
            :key="index"
            @click="selectIndex(index)"
            class="index-item"
            :class="{ selected: selectedIndices.includes(index) }"
          >
            {{ index }}
          </div>
        </div>
      </div>
      
      <div class="modal-footer">
        <button @click="close" class="btn btn-default">取消</button>
        <button 
          @click="confirmSelection" 
          class="btn btn-primary"
          :disabled="selectedIndices.length === 0"
        >
          确定
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SelectIndexToEditModal',
  props: {
    indices: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      selectedIndices: []
    };
  },
  methods: {
    selectIndex(index) {
      // 切换选中状态
      const indexPos = this.selectedIndices.indexOf(index);
      if (indexPos === -1) {
        // 如果未选中，则添加到选中列表
        this.selectedIndices.push(index);
      } else {
        // 如果已选中，则从选中列表中移除
        this.selectedIndices.splice(indexPos, 1);
      }
    },
    
    confirmSelection() {
      if (this.selectedIndices.length > 0) {
        // 只选择第一个索引进行编辑（目前只支持单个索引编辑）
        this.$emit('index-selected', this.selectedIndices[0]);
        this.close();
      }
    },
    
    close() {
      this.$emit('close');
    }
  }
};
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  max-width: 500px;
  width: 90%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #E2E8F0;
}

.modal-header h2 {
  font-size: 18px;
  color: #2D3748;
  margin: 0;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #718096;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  color: #2D3748;
}

.modal-body {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.no-indices {
  text-align: center;
  color: #718096;
  padding: 20px;
}

.indices-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.index-item {
  padding: 12px 16px;
  border: 1px solid #E2E8F0;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

.index-item:hover {
  background-color: #F7FAFC;
  border-color: #CBD5E0;
}

.index-item.selected {
  background-color: #EBF8FF;
  border-color: #4299E1;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 20px;
  border-top: 1px solid #E2E8F0;
}

.btn {
  padding: 10px 16px;
  border: none;
  border-radius: 4px;
  font-family: 'Inter', sans-serif;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background-color: #4299E1;
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background-color: #3182CE;
}

.btn-default {
  background-color: #EDF2F7;
  color: #4A5568;
}

.btn-default:hover {
  background-color: #E2E8F0;
}
</style>